<?php
namespace App\Components;

use Illuminate\Database\Capsule\Manager;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

class Providers
{
    public static function logger(string $type = 'app'): LoggerInterface
    {
        if ($type !== 'cmc') {
            $type = 'app';
        }

        $logger = new Logger($_ENV['app_name']);
        if ($type == 'app') {
            // $logger->pushProcessor(new Monolog\Processor\IntrospectionProcessor());
            $logger->pushProcessor(new \Monolog\Processor\WebProcessor());

            $stream = ROOT_PATH . 'storage/log/app-' . date('Ymd') . '.log';
        } else {
            $stream = ROOT_PATH . 'storage/log/cmc-' . date('Ymd') . '.log';
        }
        $handler = new StreamHandler($stream);
        $logger->pushHandler($handler);
        return $logger;
    }

    public static function database(): Manager
    {
        $database = new Manager();
        $database->addConnection($_ENV['database']);
        // $database->setAsGlobal();
        $database->bootEloquent();
        return $database;
    }
}
